var session=$.cookie('user_session');
var user_id=$.cookie('user_id');
//if((!user_id)||(!session)){
//	top.window.location.href = "/index.html";
//}


function httpGet(url) {
	var callback=null;
	var data=null;
	if(arguments.length==3){
		callback=arguments[2]
		data=arguments[1]
	}else{
		callback=arguments[1]
	}
	var time=(new Date()).getTime();
	var nonce=Math.random().toString(36).substr(2, 15);
	var args={
		user_id:user_id,
		session:session,
		url:"/app/" +url,
		nonce:nonce,
		time:time
	}
	if(data){
		args=Object.assign(args,data)
	}
	var keys = Object.keys(args)
	keys = keys.sort()
	var newArgs = {}
	keys.forEach(function (key) {
		newArgs[key.toLowerCase()] = args[key]
	})

	var string = ''
	for (var k in newArgs) {
		if (newArgs[k] !== undefined) {
			string += '&' + k + '=' + newArgs[k]
		}
	}
	string = string.substr(1)

	var signature = hex_sha1(string)
	$.ajax({
		type: "get",
		data:data,
		contentType:"application/json",
        		dataType:'json',
		headers:{
			user_id:user_id,
			session:session,
			sign:signature,
			nonce:nonce,
			time:time
		},
		url:  "/app/" + url,
		async: true,
		success: function(res) {
			checkSession(res);
			callback(res);
		}
	});
}

function httpGetSync(url, data, callback) {
	var time=(new Date()).getTime();
	var nonce=Math.random().toString(36).substr(2, 15);
	var args={
		user_id:user_id,
		session:session,
		url:"/app/" +url,
		nonce:nonce,
		time:time
	}
	if(data){
		args=Object.assign(args,data)
	}
	var keys = Object.keys(args)
	keys = keys.sort()
	var newArgs = {}
	keys.forEach(function (key) {
		newArgs[key.toLowerCase()] = args[key]
	})

	var string = ''
	for (var k in newArgs) {
		if (newArgs[k] !== undefined) {
			string += '&' + k + '=' + newArgs[k]
		}
	}
	string = string.substr(1)
	var signature = hex_sha1(string)
	$.ajax({
		type: "get",
		url:  "/app/" + url,
		contentType:"application/json",
		dataType:'json',
		data:data,
		headers:{
			userid:user_id,
			session:session,
			sign:signature,
			nonce:nonce,
			time:time
		},
		async: false,
		success: function(res) {
			checkSession(res);
			callback(res);
		}
	});
}

function httpPost(url, data, callback) {
	var time=(new Date()).getTime();
	var nonce=Math.random().toString(36).substr(2, 15);
	var args={
		user_id:user_id,
		session:session,
		url:"/app/" +url,
		nonce:nonce,
		time:time
	}
	if(data){
		args=Object.assign(args,data)
	}
	var keys = Object.keys(args)
	keys = keys.sort()
	var newArgs = {}
	keys.forEach(function (key) {
		newArgs[key.toLowerCase()] = args[key]
	})

	var string = ''
	for (var k in newArgs) {
		if (newArgs[k] !== undefined) {
			string += '&' + k + '=' + newArgs[k]
		}
	}
	string = string.substr(1)

	var signature = hex_sha1(string)
	$.ajax({
		type: "post",
		url:  "/app/" + url,
		contentType: "application/json",
		dataType:"json",
		data: JSON.stringify(data),
		headers:{
			user_id:user_id,
			session:session,
			sign:signature,
			nonce:nonce,
			time:time
		},
		dataType:'json',
		async: true,
		success: function(res) {
			checkSession(res);
			callback(res);
		}
	});
}


function checkSession(res) {
	if(res.code <=-99) {
		top.window.location.href = "/index.html";
	}
}

function parseUrlParams(){
	var search=window.location.search;
	if(!search||search.length<2) {
		return {}
	}
	var searchStr= search.slice(1)
	var searchParts= searchStr.split("&");
	if(searchParts.length==0){
		return {}
	}
	var urlParams={};
	for(var part of searchParts){
		var kvs=part.split("=")
		if(kvs[1].length==0){
			urlParams[kvs[0]]  =kvs[1]
		}else{
			urlParams[kvs[0]]  =decodeURIComponent(kvs[1])
		}

	}
	return urlParams
}
